import { onMounted, onUnmounted } from "vue";

function useTimeout(fn: () => void, delay: number | null | undefined) {
  let timer: number;

  onMounted(() => {
    if (delay === null || delay === undefined) return;
    timer = setTimeout(() => {
      fn();
    }, delay);
  });

  onUnmounted(() => {
    clearTimeout(timer);
  });
}

export default useTimeout;
